﻿using System;
using System.Collections.Generic;

namespace CAION2.Helpers
{
    public class MultiDictionary<T,K> : Dictionary<T, List<K>>
    {
        private void EnsureKey(T key)
        {
            if (!ContainsKey(key))
            {
                this[key] = new List<K>(1);
            }
            else
            {
                if (this[key] == null) 
                    this[key] = new List<K>(1);
            }
        }

        public void AddValue(T key, K newItem)
        {
            EnsureKey(key);
            this[key].Add(newItem);
        }

        public void AddValues(T key, IEnumerable<K> newItems)
        {
            EnsureKey(key);
            this[key].AddRange(newItems);
        }

        public bool RemoveValue(T key, K value)
        {
            if (!ContainsKey(key)) return false;

            this[key].Remove(value);

            if (this[key].Count == 0) Remove(key);
            return true;
        }

        public bool RemoveValues(T key, Predicate<K> match)
        {
            if (!ContainsKey(key)) return false;

            this[key].RemoveAll(match);

            if (this[key].Count == 0) Remove(key);
            return true;
        }
    }

    public enum Messages
    {
        StatsGameClassSend = 1,
        StatsCharacterLevelSend = 2,

        ShowCard = 3,
        MainSlotChange = 4,

        CardStatsSend = 5,
        CardStatsNullSend = 6,
        CardCorrectCheck = 7,

        ItemSetAddItem = 8,
        ItemSetDelItem = 9,
        ItemSetSend = 10,

        SetArmsfusion = 11,
        ArmsfusionSlotChange = 12,

        StigmasGameClassSend = 13,
        StigmasCharacterLevelSend = 14,
        StigmaSumsSend = 15,
        SetStigmaPlace = 16,
        EnhantedStigmaSend = 17,

        RecipeSend = 18,
        RecipeRequire = 19,

        ReceptureCalculator = 20,
        ReceptureTitleChange = 21
    };

    public sealed class Mediator
    {
        static readonly Mediator instance = new Mediator();
        private volatile object _locker = new object();

        MultiDictionary<Messages, Action<Object>> internalList = new MultiDictionary<Messages, Action<object>>();
        MultiDictionary<string, Action<Object>> internalList2 = new MultiDictionary<string, Action<object>>();
 
        static Mediator()
        {
            
        }

        private Mediator()
        {
            
        }

        public static Mediator Instance
        {
            get { return instance; }
        }

        public void Register(Action<Object> callback, Messages message)
        {
            internalList.AddValue(message, callback);
        }

        public void Register(Action<Object> callback, String message)
        {
            internalList2.AddValue(message, callback);
        }

        public void UnRegister(Action<Object> callback, String message)
        {
            internalList2.RemoveValue(message, callback);
        }

        public void NotifyColleagues(Messages message, object args)
        {
            if (!internalList.ContainsKey(message)) return;

            foreach (Action<object> callback in internalList[message])
            {
                callback(args);
            }
        }

        public void NotifyColleagues(string message, object args)
        {
            if (!internalList2.ContainsKey(message)) return;

            foreach (Action<object> callback in internalList2[message])
            {
                callback(args);
            }
        }
    }
}
